package com.olclass.web.service;

import com.olclass.web.dao.CollegeRepository;
import com.olclass.web.dao.MajorRepository;
import com.olclass.web.domain.College;
import com.olclass.web.domain.Major;
import com.olclass.web.domain.Role;
import com.olclass.web.support.QueryParamUtil;
import com.olclass.web.support.QuerySpecification;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.jmx.export.notification.ModelMBeanNotificationPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * FileName: CollegeService.java
 * Author:   Administrator
 * Date:     2021/3/23 19:12
 * Description:
 */
@Service
@Transactional
public class CollegeService {

    @Autowired
    CollegeRepository collegeRepository;

    @Autowired
    MajorRepository majorRepository;


    /**
     * 查询接口实现
     * @param filterParam
     * @param sortParam
     * @param start
     * @param limit
     * @return
     */
    public Page<College> queryByFilter(String filterParam, String sortParam, int start, int limit) {
        Page<College> results = collegeRepository.findAll(new QuerySpecification<College>(filterParam),
                new PageRequest(start, limit, QueryParamUtil.parseSortParams(sortParam)));


        return results;
    }

    public void saveCollege(College college){
        collegeRepository.save(college);
    }

    public College findById(String id){
        return  collegeRepository.findById(id);
    }

    public void saveMajor(Major major){
        majorRepository.save(major);
    }

    public List<Major> queryMajorsByPid(String pid){
        return majorRepository.queryMajorsByPid(pid);
    }

    public void deleteCollege(String id){
        collegeRepository.deleteById(id);
        majorRepository.deleteByPid(id);
    }

    public void deleteMajor(String id){
        majorRepository.deleteById(id);
    }
}